//
//  JSMMoreViewController.m
//  TakeYouTourTogether
//
//  Created by dllo on 15/12/3.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMMoreViewController.h"
#import "JSMAboutMeViewController.h"
#import "JSMSkinViewController.h"
#import "JSMSettingCell.h"

@interface JSMMoreViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)BaseTableView *myTableView;
@property(nonatomic, strong)NSArray *titleArr;
@property(nonatomic, strong)NSArray *imageArr;
@property(nonatomic, strong)NSArray *languageArr;
@property(nonatomic, strong)NSArray *lan_Arr;
@end

@implementation JSMMoreViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
        self.titleArr = @[@"一键换肤", @"夜间模式",@"关于我们"];
        self.imageArr = @[@"hf", @"dyy", @"yjms", @"gywm"];
        self.languageArr = @[@"简体中文",@"English"];
        self.lan_Arr = [LanguageTool AllLanguages];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    self.title = LanguageTool(@"设置");
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"<<" style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    [self createView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.myTableView reloadData];
}
#pragma mark 返回
- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 创建视图
- (void)createView{
    self.myTableView = [[BaseTableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.bounces = NO;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titleArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *str = @"reuse";
    JSMSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[JSMSettingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.settingLabel.text = LanguageTool(self.titleArr[indexPath.section]);
    if (indexPath.section == 1) {
        UISwitch *myWwitch = [[UISwitch alloc]initWithFrame:CGRectZero];
        myWwitch.onTintColor = [UIColor blackColor];
        myWwitch.on = ![NightTimeModel shareNightTimeModel].isDayOrNight;
        [myWwitch addTarget:self action:@selector(dayOrNight:) forControlEvents:UIControlEventTouchUpInside];
        cell.accessoryView = myWwitch;
    }
//    if (indexPath.section == 1) {
//        BaseLabel *lanLabel = [[BaseLabel alloc]initWithFrame:CGRectMake(0, 0, 60, 40)];
//        cell.accessoryView = lanLabel;
//        lanLabel.font = [UIFont systemFontOfSize:13];
//        lanLabel.textColor = [UIColor lightGrayColor];
//        lanLabel.text = self.languageArr[[LanguageTool shareLanguage].lan_num];
//    }
    cell.imageView.image = [UIImage imageNamed:self.imageArr[indexPath.section]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    BaseLabel *headerLabel = [[BaseLabel alloc]initWithFrame:CGRectMake(0, 0, VIEWWIDTH, 15)];
    headerLabel.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
    return headerLabel;
    
}
#pragma mark 夜间模式
- (void)dayOrNight:(UISwitch *)myWwitch {
    [NightTimeModel shareNightTimeModel].isDayOrNight = myWwitch.on;
    NSNumber *boolNumber = [NSNumber numberWithBool:[NightTimeModel shareNightTimeModel].isDayOrNight];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil userInfo:@{@"1": boolNumber}];
    NSLog(@"%u", myWwitch.on);
    [self.myTableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        JSMSkinViewController *skinVC = [[JSMSkinViewController alloc]init];
        skinVC.hidesBottomBarWhenPushed = YES;
        skinVC.navigationItem.hidesBackButton = YES;
        [self.navigationController pushViewController:skinVC animated:YES];
    }
//    else if(indexPath.section == 1){
//        if ([LanguageTool shareLanguage].lan_num == 1) {
//            [LanguageTool shareLanguage].lan_num = 0;
//        }else{
//            [LanguageTool shareLanguage].lan_num = 1;
//        }
//        NSString *language = self.lan_Arr[[LanguageTool shareLanguage].lan_num];
//        if ([language hasSuffix:@"-US"]) {
//            language = [language stringByReplacingOccurrencesOfString:@"-US" withString:@""];
//        }
//        [LanguageTool shareLanguage].iphoneLanguage = language;
//        [LanguageTool setUserlanguage:[LanguageTool shareLanguage].iphoneLanguage];
//        self.title = LanguageTool(@"设置");
//        NSLog(@"%ld",self.tabBarController.selectedIndex);
//        [self.myTableView reloadData];
//    }
    else if(indexPath.section == 2){
        JSMAboutMeViewController *aboutmeVC = [[JSMAboutMeViewController alloc]init];
        aboutmeVC.navigationItem.hidesBackButton = YES;
        aboutmeVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:aboutmeVC animated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
